package j2metris.ui;

import j2metris.Coordinate;
import j2metris.ControllablePiece;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Contains the routines used to draw various game components.
 */
class Drawer
{
    private static final int topLeftAnchor = Graphics.TOP | Graphics.LEFT;
    static void drawUnit(Graphics g, Image image,
            int distanceFromLeftEdgeOfScreenToLeftEdgeOfWell,
            int distanceFromBottomOfScreenToBottomOfWell, Coordinate position,
            int unitWidth, int unitHeight)
    {
        g.drawImage(
                image,
                (position.getX() * unitWidth) + distanceFromLeftEdgeOfScreenToLeftEdgeOfWell,
                (position.getY() * unitHeight) + distanceFromBottomOfScreenToBottomOfWell,
                topLeftAnchor);
//        if(J2metris.DEBUG) System.err.println("UNIT DRAWN");
    }

    static void drawPiece(Graphics g, ControllablePiece piece,
            int distanceFromLeftEdgeOfScreenToLeftEdgeOfWell,
            int distanceFromBottomOfScreenToBottomOfWell, Coordinate position,
            int unitWidth, int unitHeight)
    {
        Coordinate[] rotationState = piece.getRotationState();
        Image unitImage = piece.getUnitImage();
        for(int i = 0; i < rotationState.length; ++i)
        {
            drawUnit(g, unitImage, distanceFromLeftEdgeOfScreenToLeftEdgeOfWell,
                    distanceFromBottomOfScreenToBottomOfWell, position.add(rotationState[i]),
                    unitWidth, unitHeight);
//            System.err.println("DRAWING UNIT " + i);
        }
    }
}
